package com.suning.sawp.web.controller;

import java.io.IOException;
import java.net.URLDecoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.framework.web.gson.GsonView;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.service.impl.tm.weixin.WeixinConfigService;
import com.suning.sawp.service.impl.tm.weixin.WeixinJsSdkConfig;
/**
 * 
 * 轻应用-微信相关联业务控制类<br>
 * 〈功能详细描述〉
 * 
 * @author 12061818
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/weixin")
public class WeixinConfigController {

    /**
     * 是否成功处理标志位
     */
    public static final String RET_FLAG = "retFlag";
    /**
     * 成功处理状态
     */
    public static final String SUCCESS = "1";
    /**
     * 失败处理状态
     */
    public static final String FAILED = "0";
    /**
     * 错误码
     */
    public static final String ERROR_CODE = "errorCode";
    /**
     * 错误消息
     */
    public static final String ERROR_MSG = "errorMessage";

    private static final Logger LOGGER = LoggerFactory.getLogger(WeixinConfigController.class);

    @Resource
    WeixinConfigService weixinConfigService;

    // 设置返回对象
    private static class MyGsonView extends GsonView {

        private static final String CONTENT_TYPE = "application/json;charset=utf-8";

        @Override
        public String getContentType() {
            return CONTENT_TYPE;
        }

    }

    /**
     * 
     * 功能描述: 获取微信JS-SDK config所需的配置参数<br>
     * 〈功能详细描述〉
     * 
     * @param url
     * @param appId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping(value = "/getjsapiconfig.htm")
    public GsonView getjsapiconfig(String url, String appId, HttpServletResponse response) throws IOException {
        LOGGER.debug("appId:{}, url:{}", appId, url);

        GsonView gsonView = new MyGsonView();
        gsonView.addStaticAttribute(RET_FLAG, SUCCESS);
        gsonView.addStaticAttribute(ERROR_CODE, StringUtils.EMPTY);
        gsonView.addStaticAttribute(ERROR_MSG, StringUtils.EMPTY);
        ReturnMsg<WeixinJsSdkConfig> ret = new ReturnMsg<WeixinJsSdkConfig>();
        if (StringUtils.isBlank(appId) || StringUtils.isBlank(url)) {
            // 检查参数是否为空
            ret.setError(ErrorCodeConstants.ERR_1001 ,"参数错误");
        } else {
            // 调用公众号接口获取jsticket
            String jsticket = weixinConfigService.getJsticket(appId);
            if (StringUtils.isBlank(jsticket)) {
                // 获取jsticket为空
                ret.setError(ErrorCodeConstants.ERR_1003, "获取jsticket失败");
            } else {
                // weixinJS-SDK 设置
                String realUrl = URLDecoder.decode(url, "UTF-8");
                WeixinJsSdkConfig config = weixinConfigService.getWeixinJsSdkConfig(appId, jsticket, realUrl);
                ret.setData(config);
            }
        }

        gsonView.addStaticAttribute("ret", ret);
        gsonView.setJsonObjectName("ret");
        response.setHeader("Access-Control-Allow-Origin", "*");
        return gsonView;
    }
}
